/**
 * class FileDatabase
 * This class serves as an abstraction to separate the DataManager from the FileHandler
 * It serves as a link between the live storage and the permanent storage
 * All updates to permanent storage go through this
 * All access to permanent storage must be called from here
 * 
 * @author Ankit Ganla
 * @author Vaarnan Drolia
 * 
 */

package storage;


import java.io.File;

import core.Log;

import data.EventList;

public class FileDatabase
{
	static String nameOfDirectory = "userData";
	
	public FileDatabase() {
		createDirectory(nameOfDirectory);
	}

	/**
	 * 
	 * @param eventList
	 * @return boolean - true if written, false if not written
	 */
	public boolean writeEventList(EventList eventList) {
		Log.debug("FileDB: Entering the writeList function");
		String destination = nameOfDirectory + File.separator + eventList.getName();
		if(!FileHandler.writeObject(eventList, destination)) {
			Log.warn("FileDB: In writeList. FileHandler.writeObject returned false. Object was not written to storage.");
			return false;
		}
		return true;
	}
	
	/**
	 * 
	 * @param directoryName
	 * @return boolean - true if directory is created, false if not created
	 */
	private boolean createDirectory(String directoryName) {
		Log.debug(" FileDB: Entering the createDirectory function");
		File dir = new File(directoryName);
		if(dir.isDirectory()) {
			Log.warn("FileDB: directory not created because it already exsists");
			return false;
		}
		Log.debug("FileDB: Directory created");
		return dir.mkdir();
	}

	/**
	 * 
	 * @return String array with names of all lists
	 */
	public String[] getListNames() {
		Log.debug(" FileDB: Entering getListnames function");
		String[] namesOfLists;
		File dir = new File(nameOfDirectory);
		namesOfLists = dir.list();
		return namesOfLists;
	}

	/**
	 * 
	 * @return boolean- true if database is cleared and recreated, false if not
	 */
	public boolean clearDatabase() {
		Log.debug("FileDB: Entering cleardatabase");
		File dir = new File(nameOfDirectory);
		boolean flag = true;
		File[] dataFiles = dir.listFiles();
		for (File file : dataFiles) {
			if (!file.delete()) {
				Log.warn("FileDB: File was not deleted when it should have been in clearDatabase");
				flag = false;
			}
		}
		if (!dir.delete()) {
			Log.warn("FileDB: directory was not deleted in clearDatabase function");
			return false;
		}
		if (!createDirectory(nameOfDirectory)) {
			Log.warn("Directory was not recreated in clearDatabase function");
			return false;
		}
		return flag;
	}
	
	/**
	 * 
	 * @param listName
	 * @return EventList object
	 */
	public EventList readList(String listName) {
		String destination = nameOfDirectory + File.separator + listName;
		EventList eventList = (EventList) FileHandler.readObject(destination);
		return eventList;
	}

	/**
	 * Sends string to FileHandler to be written to a file
	 * @param toBeWritten
	 */
	public void exportToText(String toBeWritten) {
		Log.debug("FileDB: Entering exportTotext");
		FileHandler.writeToText(toBeWritten);
	}

	/**
	 * 
	 * @return boolean - true if old text file is cleared, false otherwise
	 */
	public boolean clearOldText() {
		Log.debug("FileDB: Entering clearOldText");
		if(!FileHandler.removeOldText()) {
			Log.warn("FileDB: removeOldText in FileHandler returned a false. File could not be cleared in clearOldText");
			return false;
		}
		return true;
	}

}
